package util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class HttpUtils {

    public static String doRemoteGet(String url,String message)
    {
        HttpGet httpRequest = new HttpGet(url);
        if(StringUtils.isNotBlank(message)){
            httpRequest.setHeader("message",message);
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = null;
        try {
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            if(httpResponse.getStatusLine().getStatusCode()==200){
                HttpEntity httpEntity = httpResponse.getEntity();
                result = EntityUtils.toString(httpEntity);//取出应答字符串
            }
        }catch (Exception e){
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

}
